package com.nova.bluetooth.helpUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.nova.bluetooth.R;


/**
 * CustomView Toast
 */
public class CustomViewToastUtil {
    private static final int LayoutID = R.layout.toast_view_show_message;
    private CustomViewToastUtil() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    private static Context context;
    // Toast对象
    private static Toast toast;
    private static TextView tipsText;
    /**
     * 在Application中初始化ToastUtils.init(this)
     *
     * @param context
     */
    public static void init(Context context) {
        CustomViewToastUtil.context = context.getApplicationContext();
        if (toast == null) {
            toast = new Toast(context);
            View view = LayoutInflater.from(context).inflate(LayoutID, null, false);
            tipsText = view.findViewById(R.id.tv_show_bt_message);
            toast.setView(view);
        }

    }

    /**
     * 发送Toast,默认LENGTH_SHORT
     */
    public static void show(String str) {
        showToast(context, str, Toast.LENGTH_SHORT);
    }

    private static void showToast(Context context, String massage, int duration) {
        // 设置显示文字的颜色
        if (toast == null) {
            init(context);
        } else {
            tipsText.setText(massage);
            toast.setDuration(duration);
        }
        toast.show();
    }

    /**
     * 在UI界面隐藏或者销毁前取消Toast显示
     */
    public static void cancel() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }
}
